package com.gzzq.controller;

import java.util.Map;

import javax.jms.JMSException;
import javax.jms.Message;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gzzq.entity.QueueType;
import com.gzzq.util.ResponseUtil;

/**
 * 队列 ：出队
 * 
 * @author huanghp
 *
 */
@Controller
@RequestMapping(value = "/receiver")
public class ReceiverController {
	@Autowired
	JmsTemplate jmsTemplate;
	@Autowired
	MessageConverter messageConverter;
	static final Logger LOG = LoggerFactory.getLogger(ReceiverController.class);

	@GetMapping(value = "/get/{queueType}")
	@ResponseBody
	public Map<String, Object> receiveMethod(@PathVariable(value = "queueType") String queueType) {

		if (StringUtils.isBlank(queueType)) {
			return ResponseUtil.falseBizResult(">>>>>>>请传入参数队列类型:>>>>>>>>>>:queueType");
		}
		if ("1".equals(queueType)) {
			QueueType type = QueueType.INTEGRAL_QUEUE;
			queueType = type.toString();
		}
		if ("2".equals(queueType)) {
			QueueType type = QueueType.MESSAGE_QUEUE;
			queueType = type.toString();
		}
		try {
			//jmsTemplate.setPriority(2);
		//while() 队列
			Message abc = jmsTemplate.receive(queueType);
		//	System.out.println(abc.getJMSPriority());
			String message = (String) messageConverter.fromMessage(abc);
			if (StringUtils.isBlank(message)) {
				return ResponseUtil.falseBizResult(">>>>>>>>>>队列：" + queueType + "的消息已被取完");
			}
			return ResponseUtil.falseBizResult("接收了队列" + queueType + "的消息", message);
		} catch (MessageConversionException e) {
			e.printStackTrace();
			return ResponseUtil.falseBizResult("消息接收异常,请检查服务！！！");
		} catch (JMSException e) {
			e.printStackTrace();
			return ResponseUtil.falseBizResult("消息接收异常,请检查服务！！！");
		}
	}

}
